<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class BusinesssetController extends AdminbaseController{
	
	function _initialize() {
		parent::_initialize();
	}

	/**
	 * 业务设置 list
	 */
	function active_reward(){

		$common = include 'application/Common/Conf/common.php';

		$data = array_change_key_case($common,CASE_LOWER);// 把键名改为全小写

		$this->assign("data", $data);
		$this->display();
	}

	/**
	 * 业务设置 修改
	 */
	function active_reward_post(){

		$ret = sp_save_var('application/Common/Conf/common.php',$_POST);

		if($ret){
			$this->success("成功！");

		}
	}
	/**
	 * 用户设置列表=================================
	 */
	function user_set()
	{
		$water_db = M('user_set u');

		$pay_account=  I("post.mobile");//支付账号

		$formget=array('mobile'=>$pay_account);

		$where = " 1=1 ";

		if(!empty($pay_account))
			$where = $where." and au.mobile like '%".$pay_account."%'";

		$count = $water_db->where($where)->join('db_app_users au ON u.user_id=au.user_id')->count();
		import("Org.Util.Pagemumayi");
		$page = new \Pagemumayi($count,20);
		//分页跳转的时候保证查询条件
		foreach($formget as $key=>$val) {
			$page->parameter .= "$key=".urlencode($val)."&";
		}

		$data = $water_db->field('au.mobile,u.*')->join('db_app_users au ON u.user_id=au.user_id')
			->limit($page->firstRow.','.$page->listRows)
			->where($where)
			->order(' mobile')
			->select();

		$this->assign('datas',$data);
		$this->assign('page',$page->show('Admin'));
		$this->assign("formget",$formget);
		$this->display();
	}

	/**
	 * 保价设置
	 * 加急设置
	 */
	function sysset_list(){
		$sys_set_db = M('sys_set');

		$sys_sets = $sys_set_db->select();

		if(!$sys_sets)
			$this->display();

		foreach($sys_sets as $key=>$value){
			$ret_array = null;
			$count = $key + 1 ;
			if($value['set_id']==$count){
				$ret_array = json_decode($value['collection_cost'], true);
				$this->assign('data_'.$count,$ret_array);
			}
		}

		$this->display();
	}
	/**
	 * 保价设置
	 * 加急设置
	 */
	function sysset_update(){

		$sys_set_db = M('sys_set');

		$post_text = I('post.');

		if(!$post_text)
			$this->success("没有数据！");
		$set_id = $post_text['set_id'];
		$data_text = $post_text['text'.$set_id];

		if( !$data_text )
			$this->success("您没有修改哦~！");

		foreach($data_text as $key=>$value){
			if(!$value)
				continue;
			$data_array[$key]['text']=$value;
			$data_array[$key]['status']='normal';
		}
		$data_json = json_encode($data_array);
		$data['collection_cost']=$data_json;

		$data_have = $sys_set_db->where('set_id='.$set_id)->find();

		if(!$data_have){
			$data['set_id']=$set_id;
			$ret_add_bj = $sys_set_db->add($data);
			if(!$ret_add_bj)
				$this->error("添加失败！");
		}
		else{
			$ret_update_bj =  $sys_set_db->where('set_id='.$set_id)->save($data);
			if(!$ret_update_bj)
				$this->success("您没有修改哦~！");
		}

		$this->success("设置成功！");
	}


	/**
	 * 服务价格策略
	 */
	function pricing_tactics_list(){
		$pricing_tactics_db = M('pricing_tactics');
		$province_id=  I("post.city");//省
		$city_id=  I("post.city_id");//市

		$formget=array('$city'=>$province_id
						,'city_id'=>$city_id
					);

		$where = '1';
		if($province_id)
			$where =$where.' and province_id = '.$province_id;

		if($city_id)
			$where = $where.' and city_id = '.$city_id;

		$sys_sets = $pricing_tactics_db->where($where)->select();

		$this->assign('datas',$sys_sets);
		$this->assign('formget',$formget);
		$province = M('province');
		$this->assign('province_data', $province->select());
		$this->display();
	}
	function pricing_tactics_add(){
		$pricing_tactics_db = M('pricing_tactics');

		if (IS_POST) {
			$post_data = I('post.');

			if(!$post_data)
				$this->error("请输入数据1");

			$city_id = $post_data['city_id'];
			if(!$city_id)
				$this->error("请选择城市！");
			$province_id = $post_data['city'];

			$pricing = $pricing_tactics_db->field('pid')->where(' city_id = '.$city_id)->find();

			if($pricing && $pricing['pid'])
				$this->error("您选择的城市设置已经存在，请重新选择！");

			$data_json = $this->getpricing_tactics_data_json($post_data['mileage'],$post_data['line_up'],$post_data['night'],$post_data['temporary'],$post_data['is_starting_price']);
			if(!$data_json)
				$this->error("请填写完整！");

			$data['tactics_array'] =$data_json;
			$data['city_id']=$city_id;
			$data['province_id']=$province_id;

			$ret_add_bj = $pricing_tactics_db->add($data);
			if(!$ret_add_bj)
				$this->error("添加失败！");
			else
				$this->success("添加成功",U('Businessset/pricing_tactics_list'));
		} else {
			$province = M('province');
			$this->assign('province_data', $province->select());
			$this->display();
		}

	}
	/**
	 * 保价设置
	 * 加急设置
	 */
	function pricing_tactics_update(){

		$sys_set_db = M('pricing_tactics');

		if (IS_POST) {

			$post_data = I('post.');

			$data['pid'] = $post_data['pid'];

			if(!$data['pid'])
				$this->error("数据错误！");

			$data_json = $this->getpricing_tactics_data_json($post_data['mileage'],$post_data['line_up'],$post_data['night'],$post_data['temporary'],$post_data['is_starting_price']);

			if(!$data_json)
				$this->error('请填写好数据');

			$data['tactics_array']=$data_json;

			$ret = $sys_set_db->where('pid = '.$post_data['pid'])->save($data);

			if ($ret) {
				$this->success("修改成功！", U("pricing_tactics_list"));
			} else {
				$this->error("修改失败！");
			}
		} else {

			$ret = $sys_set_db->where(array('pid' => I('pid')))->find();
			if(!$ret)
				$this->error("此数据不存在");

			$data_arr = json_decode( $ret['tactics_array'],true);
			$this->assign('mileage', $data_arr['mileage']);
			$this->assign("line_up", $data_arr['line_up']);
			$this->assign("night", $data_arr['night']);
			$this->assign("temporary", $data_arr['temporary']);
			$this->assign("is_starting_price", $data_arr['is_starting_price']);
			$this->assign("data", $ret);

			$province = M('province');
			$this->assign('province_data', $province->select());

			$citys = getCityList($ret['province_id']);
			$this->assign('citys', $citys);

			$this->display();
		}
	}
	/**
	 * 删除服务策略设置
	 */
	function pricing_tactics_delete()
	{
		$sys_set_db = M('pricing_tactics');
		$ret = $sys_set_db->where('pid='.I('pid'))->delete();

			if ($ret) {
				$this->success("删除成功！", U("pricing_tactics_list"));
			} else {
				$this->error("删除失败！", U("pricing_tactics_list"));
			}
	}
	function pricing_tactics_see(){
		$sys_set_db = M('pricing_tactics');

		$ret = $sys_set_db->where(array('pid' => I('pid')))->find();
		if(!$ret)
			$this->error("此数据不存在");

		$data_arr = json_decode( $ret['tactics_array'],true);

		$this->assign('mileage', $data_arr['mileage']);
		$this->assign("line_up", $data_arr['line_up']);
		$this->assign("night", $data_arr['night']);
		$this->assign("temporary", $data_arr['temporary']);
		$this->assign("is_starting_price", $data_arr['is_starting_price']);
		$this->assign("data", $ret);

		$this->display();

	}

	/**
	 * 将post参数值，连接成一个数组，并转为json字符串返回
	 * @param $mileage 里程费
	 * @param $line_up 排队费
	 * @param $night  夜间加价
	 * @param $temporary 临时加价
	 * @param $is_starting_price 其他
	 * @return int|string 返回json字符串
	 */
	function getpricing_tactics_data_json($mileage,$line_up,$night,$temporary,$is_starting_price){
		//里程费
		$mileage_arr = array();
		$mileage_count = 0;
		if($mileage){
			foreach($mileage as $key=>$value){
				if(!$value||!$value['title'])
					continue;

				$mileage_arr['mileage'][$mileage_count] =$value;
				$mileage_count++ ;
			}
		}
		//排队费
		$line_up_arr = array();
		$line_up_count = 0;
		if($line_up){
			foreach($line_up as $key=>$value){
				if(!$value||!$value['title'])
					continue;
				$line_up_arr['line_up'][$line_up_count] =$value;
				$line_up_count++ ;
			}
		}

		//夜间补助
		$night_arr = array();
		$night_count = 0;
		if($night){
			foreach($night as $key=>$value){
				if(!$value||!$value['title'])
					continue;
				$night_arr['night'][$night_count] =$value;
				$night_count++ ;
			}
		}

		//起步价
		$starting_price_arr = array();
		$starting_price_count = 0;
		if($is_starting_price){
			foreach($is_starting_price as $key=>$value){
				if(!$value||!$value['mileageKilometre']||!$value['mileageMoney'])
					continue;
				$starting_price_arr['is_starting_price'][$starting_price_count] =$value;
				$starting_price_count++ ;
			}
		}

		if(!$mileage_arr && !$line_up_arr && !$night_arr && !$temporary && !$starting_price_arr)
			return 0;

		$data_arr = array_merge($mileage_arr,$line_up_arr,$night_arr,array('temporary'=>$temporary) ,$starting_price_arr);

		$data_json = json_encode($data_arr);

		return $data_json;
	}

	/**
	 * 转盘奖项设置
	 */
	function randset_list(){
		$sys_set_db = M('sys_set');

		$set_id = I('set_id');
		$sys_sets = $sys_set_db->where(array('set_id'=>$set_id))->find();

		if(!$sys_sets)
			$this->display();

		$ret_array = json_decode($sys_sets['collection_cost'], true);

		$this->assign('datas',$ret_array);

		$this->display();
	}
	/**
	 * 转盘奖项设置修改
	 */
	function randset_update(){

			$sys_set_db = M('sys_set');

			$post_data = I('post.');

			$set_id = $post_data['set_id'];

			if(!$set_id)
				$this->error("数据错误！");

			$data_json = $this->get_sysset_json($post_data['datas']);

			if(!$data_json)
				$this->error('请填写好数据');

			$data['collection_cost']=$data_json;

			$ret = $sys_set_db->where('set_id = '.$set_id)->save($data);

			if ($ret) {
				$this->success("设置成功！");
			} else {
				$this->error("修改失败！");
			}
	}
	/**
	 * 将post参数值，连接成一个数组，并转为json字符串返回
	 * @param $mileage 里程费
	 * @return int|string 返回json字符串
	 */
	function get_sysset_json($mileage)
	{
		$mileage_arr = array();
		$mileage_count = 0;
		if ($mileage) {
			foreach ($mileage as $key => $value) {
				if (!$value || !$value['id'])
					continue;

				$mileage_arr[$mileage_count] = $value;
				$mileage_count++;
			}
		}
		$data_json = json_encode($mileage_arr);
		return $data_json;
	}
}